package com.hxqh.services;

import java.util.ArrayList;
import java.util.List;

import com.hxqh.bo.PolicyInfoBO;
import com.hxqh.bo.dao.FpsPolicyInfoDAO;
import com.hxqh.bo.dao.FpsTradeLogDAO;
import com.hxqh.bo.dao.FpsUserPolicySelectedDAO;
import com.hxqh.services.constaints.ServiceConstains;

public class FpsViewService {

	private FpsPolicyInfoDAO fpsPolicyInfoDao;
	private FpsTradeLogDAO fpsTradeLogDao;
	private FpsUserPolicySelectedDAO fpsUserPolicySelectedDao;
	
	public List getMostActiveTop5(int type,String intervalDay) {
		String sql = "";
		switch(type) {
		case 1:
			if(!intervalDay.equals("all")) {
				sql = "SELECT b.POLICY_ID,count(POLICY_ORDER_NO) ORDER_NO" +
						" FROM fps_trade_log a, fps_policy_info b where b.DELETED = 0 and a.POLICY_ID = b.POLICY_ID" +
						" and date_sub(curdate(), INTERVAL "+intervalDay+" DAY) <= date(`TRADE_DATE`)" +
						" group by b.POLICY_NAME order by ORDER_NO DESC limit 3";
			}else {
				sql = "SELECT b.POLICY_ID,count(POLICY_ORDER_NO) ORDER_NO" +
				" FROM fps_trade_log a, fps_policy_info b where b.DELETED = 0 and  a.POLICY_ID = b.POLICY_ID" +
				" group by b.POLICY_NAME order by ORDER_NO DESC limit 3";
			}
			break;
		case 2:
			sql = "SELECT POLICY_ID FROM fps_policy_statistics_info a, fps_policy_info b" +
					" where b.DELETED = 0 and  a.POLICY_ID = B.POLICY_ID order by USER_CHOOSE_NUM desc limit 3";
			break;
		case 3:
			sql = "SELECT POLICY_ID FROM fps_policy_statistics_info a, fps_policy_info b" +
					" where b.DELETED = 0 and  a.POLICY_ID = B.POLICY_ID order by RETURN_RATE desc limit 3";
			break;
		}
		return fpsTradeLogDao.getTop5(sql);
	}

	public List<PolicyInfoBO> getPolicyInfo(String userId) {
		List policyInfo = fpsPolicyInfoDao.findAllPolicy();
		List<PolicyInfoBO> policyList = new ArrayList<PolicyInfoBO>();
		for(int i = 0; i < policyInfo.size();i ++) {
			Object[] info = (Object[]) policyInfo.get(i);
			PolicyInfoBO dataGrid = new PolicyInfoBO();
			dataGrid.setPolicyId((Integer)info[0]);
			dataGrid.setPolicyName((String)info[1]);
			dataGrid.setFuturesCode((String)info[2]);
			dataGrid.setFuturesType((String)info[3]);
			dataGrid.setTradeAllNum((Integer)info[4]);
			dataGrid.setMaxKuiFloat((Double)info[5]);
			dataGrid.setMaxHoldNum((Integer)info[6]);
			dataGrid.setFundYingkui((Double)info[7]);
			dataGrid.setReturnRate((Double)info[8]);
			boolean selected = fpsUserPolicySelectedDao.getUserSelectedPolicyId(userId, dataGrid.getPolicyId());
			dataGrid.setSelect(selected);
			policyList.add(dataGrid);
		}
		System.out.println("----policyList----"+policyInfo.size());
		return policyList;
	}
	
	public Boolean[] getSystemStatus() {
		Boolean[] status = new Boolean[2];
		status[0] = ServiceConstains.TRADING_RUNING_FLAG;
		status[1] = ServiceConstains.CTP_TRADEING_FLAG;
		return status;
	}

	public FpsPolicyInfoDAO getFpsPolicyInfoDao() {
		return fpsPolicyInfoDao;
	}

	public void setFpsPolicyInfoDao(FpsPolicyInfoDAO fpsPolicyInfoDao) {
		this.fpsPolicyInfoDao = fpsPolicyInfoDao;
	}

	public FpsUserPolicySelectedDAO getFpsUserPolicySelectedDao() {
		return fpsUserPolicySelectedDao;
	}

	public void setFpsUserPolicySelectedDao(
			FpsUserPolicySelectedDAO fpsUserPolicySelectedDao) {
		this.fpsUserPolicySelectedDao = fpsUserPolicySelectedDao;
	}

	public FpsTradeLogDAO getFpsTradeLogDao() {
		return fpsTradeLogDao;
	}

	public void setFpsTradeLogDao(FpsTradeLogDAO fpsTradeLogDao) {
		this.fpsTradeLogDao = fpsTradeLogDao;
	}

}
